package com.valuedaas.schoolsafety.controller.resource;

import com.valuedaas.schoolsafety.common.base.Result;
import com.valuedaas.schoolsafety.common.enums.DataStatus;
import com.valuedaas.schoolsafety.controller.ControllerHelper;
import com.valuedaas.schoolsafety.controller.resource.vo.InterfaceVO;
import com.valuedaas.schoolsafety.controller.resource.vo.MenuVO;
import com.valuedaas.schoolsafety.controller.user.vo.User;
import com.valuedaas.schoolsafety.service.recources.InterfaceService;
import com.valuedaas.schoolsafety.service.recources.MenuService;
import com.valuedaas.schoolsafety.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/resource/", produces = {
        "application/json;charset=utf-8"})
@Api(description = "权限管理-菜单、接口管理", value = "InterfaceController")
public class ResourceController {

    @Autowired
    private MenuService menuService;

    @Autowired
    private InterfaceService interfaceService;

    @ApiOperation(value = "获取用户拥有的所有菜单（超级管理员返回所菜单）", notes = "获取用户拥有的所有菜单，前端根据这个动态生成导航菜单")
    @RequestMapping(path = "menu/navigate.do", method = RequestMethod.GET)
    public String menuNavigate() {
        User user = WebUtil.getSessionUser();
        List<Document> menus = null;
        if (user != null && user.getUsername().equals("admin")) {
            menus = menuService.getAllMenu();
        } else {
            menus = menuService.getUserAuthedMenu(user);
        }
        return Result.ok(null, menus);
    }

    @ApiOperation(value = "获取所有菜单", notes = "获取所有菜单,管理员管理菜单时需要查询所所有的菜单")
    @RequestMapping(path = "menu/all.do", method = RequestMethod.GET)
    public String menuList() {
        List<Document> menus = null;
        menus = menuService.getAllMenu();
        return Result.ok(null, menus);
    }

    @ApiOperation(value = "获取某个菜单拥有的所有接口", notes = "获取某个菜单拥有的所有接口")
    @RequestMapping(value = "menu/interface/list.do", method = RequestMethod.GET)
    public String getInterfaceByMenuId(@RequestParam(name = "menuId", required = true) String menuId) {
        List<Document> interfaceList = interfaceService.getInterfaceByMenu(Arrays.asList(new ObjectId(menuId)));
        return Result.ok("查询成功", interfaceList);
    }

    @ApiOperation(value = "添加一个菜单", notes = "新增菜单")
    @RequestMapping(value = "menu/add.do", method = RequestMethod.POST)
    public String addMenu(@RequestBody @Valid MenuVO menu, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error("验证失败", ControllerHelper.errorsInfo(result));
        }
        menu.setState(DataStatus.NORMAL);
        menuService.add(menu);
        return Result.ok("添加成功", null);
    }

    @ApiOperation(value = "获取菜单详情", notes = "获取菜单详情")
    @RequestMapping(value = "menu/detail.do", method = RequestMethod.GET)
    public String menuDetail(@RequestParam(required = true, name = "menuId") String menuId) {
        Document menu = menuService.get(menuId);
        return Result.ok("success", menu);
    }

    @ApiOperation(value = "删除一个菜单", notes = "删除菜单")
    @RequestMapping(value = "menu/delete.do", method = RequestMethod.POST)
    public String deleteById(@RequestParam(required = true) String menuId) {
        menuService.delete(menuId);
        return Result.ok("删除成功", null);
    }

    @ApiOperation(value = "更新菜单信息", notes = "更新菜单信息")
    @RequestMapping(value = "menu/update.do", method = RequestMethod.POST)
    public String updateById(@RequestBody @Valid MenuVO menu, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error("验证失败", ControllerHelper.errorsInfo(result));
        }
        menuService.update(menu);
        return Result.ok("更新成功", null);
    }

    @ApiOperation(value = "新增一个接口", notes = "新增一个接口信息")
    @RequestMapping(value = "interface/add.do", method = RequestMethod.POST)
    public String addInterface(@RequestBody @Valid InterfaceVO interfacevo, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error("验证失败", ControllerHelper.errorsInfo(result));
        }
        interfacevo.setState(DataStatus.NORMAL);
        interfaceService.add(interfacevo);
        return Result.ok("添加成功", null);
    }

    @ApiOperation(value = "删除一个接口", notes = "删除一个接口信息")
    @RequestMapping(value = "interface/delete.do", method = RequestMethod.POST)
    public String deleteInterface(@RequestParam(required = true) String id) {
        interfaceService.delete(id);
        return Result.ok("删除成功", null);
    }

    @ApiOperation(value = "查询接口详情", notes = "根据接口id，获取接口详情")
    @RequestMapping(value = "interface/detail.do", method = RequestMethod.GET)
    public String interfaceDetail(@RequestParam(required = true) String id) {
        Document inter = interfaceService.get(id);
        return Result.ok("success", inter);
    }

    @ApiOperation(value = "更新接口信息", notes = "更新接口信息")
    @RequestMapping(value = "interface/update.do", method = RequestMethod.POST)
    public String updateInterface(@RequestBody @Valid InterfaceVO interfacevo, BindingResult result) {
        if (result.hasErrors()) {
            return Result.error("验证失败", ControllerHelper.errorsInfo(result));
        }
        interfaceService.update(interfacevo);
        return Result.ok("更新成功", null);
    }
}
